
/*
 * ClienteRMI.java
 *
 * Created on 17 de agosto de 2007, 10:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Cliente;
import java.util.*;
import Interfaces.ConsultaJdbcInterface;
import java.rmi.*;
import utiles.*;
/**
 *
 * @author EPSGRAU
 */
public class ClienteRMI {    
    /** Creates a new instance of ClienteRMI */
    public ClienteRMI( String rmiHost, String port, String service ) {            
        this.estableceSeguridad();
        this.rmiHost = rmiHost;
        this.port = port;
        this.service = service;        
    }
    
    private void estableceSeguridad() {
        try{
            Properties propSistema = Propiedades.leePropPrincipal();

            //System.setProperty("java.security.policy", "/home/Franciscorl86/java.policy");
            //System.setProperty("java.security.policy", "c:/java.policy");
            System.setProperty("java.security.policy", Propiedades.getJavaPolicy());
    //        System.setProperty ("java.rmi.server.codebase", "file:///C://serv//" );
            //System.setProperty ("java.rmi.server.codebase", "http://127.0.0.1/serv/" );
            //System.setProperty ("java.rmi.server.codebase", "http://192.168.23.40/serv/" );
            System.setProperty ("java.rmi.server.codebase", Propiedades.getServidorCodebase() );
            //System.setProperty ("java.rmi.server.codebase", "http://192.168.1.7/serv/" );
            if (System.getSecurityManager() == null){
                System.setSecurityManager(new RMISecurityManager());
            }
        }catch(Exception e){
            utiles.Utiles_MensajesDialogo.mensajeError("Error al establecer seguridad\n"+e.toString());
            e.printStackTrace();
        }
    }            

    public String getRmiHost(){
        return rmiHost;
    }
    
    public String getPort(){
        return port;
    }
    
    public String getService(){
        return service;
    }
    
    public String[][] getConsulta( String cons, String id ){
        ConsultaJdbcInterface consulta = null;
        String[][] arr = null;
        try{
            consulta = ( ConsultaJdbcInterface ) java.rmi.Naming.lookup( "//" + this.getRmiHost() + 
                        ":" + this.getPort() + "/" + this.getService() );            
            arr = consulta.getDatos( cons,id );
        }catch( Exception er ){
            System.out.println( "Error en la llamada" );
            er.printStackTrace();            
        }       
        return arr;
    }
    
    public ArrayListTableModel getTablaRegistrosSelectiva( String cons, int[] col, String id ){
        ArrayListTableModel modelo = null;
        String[][] datos = this.getConsulta( cons, id );
        modelo = new ArrayListTableModel( datos, col );        
        return modelo;        
    }
    
    public ArrayList getLista( String cons , String id ){
        ConsultaJdbcInterface consulta = null;
        ArrayList arr = null;
        try{
            consulta = ( ConsultaJdbcInterface ) java.rmi.Naming.lookup( "//" + this.getRmiHost() + 
                        ":" + this.getPort() + "/" + this.getService() );            
            arr = consulta.getListaDatos( cons, id );
        }catch( Exception er ){
            System.out.println( "Error en la llamada" );
            er.printStackTrace();            
        }       
        return arr;
    }
        
    public String[][] getConsultaUser( String cons ){
        ConsultaJdbcInterface consulta = null;
        String[][] arr = null;
        try{
            consulta = ( ConsultaJdbcInterface ) java.rmi.Naming.lookup( "//" + this.getRmiHost() + 
                        ":" + this.getPort() + "/" + this.getService() );            
            arr = consulta.getDatosConUser( cons );
        }catch( Exception er ){
            System.out.println( "Error en la llamada" );
            er.printStackTrace();            
        }       
        return arr;
    }
    
    public ArrayList getListaUser( String cons ){
        ConsultaJdbcInterface consulta = null;
        
        ArrayList arr = null;
        try{
            consulta = ( ConsultaJdbcInterface ) java.rmi.Naming.lookup( "//" + this.getRmiHost() + 
                        ":" + this.getPort() + "/" + this.getService() );            
            arr = consulta.getListaDatosConUser( cons );
        }catch( Exception er ){
            System.out.println( "Error en la llamada" );
            er.printStackTrace();            
        }       
        return arr;
    }
    
    public ArrayListTableModel getTablaRegistrosUser( String cons ){
        ArrayListTableModel modelo = null;
        ArrayList datos = this.getListaUser( cons );
        ArrayList identificadores = ( ArrayList ) datos.remove( datos.size() - 1 );
        ArrayList clases = ( ArrayList ) datos.remove( datos.size() - 1 );
        modelo = new ArrayListTableModel( datos, identificadores, clases );        
        return modelo;        
    }
    
    public ArrayListTableModel getTablaRegistros( String cons, String id ){
        ArrayListTableModel modelo = null;
        ArrayList datos = this.getLista( cons , id );
        ArrayList identificadores = ( ArrayList ) datos.remove( datos.size() - 1 );
        ArrayList clases = ( ArrayList ) datos.remove( datos.size() - 1 );
        modelo = new ArrayListTableModel( datos, identificadores, clases );        
        return modelo;        
    }   
    
    public ConsultaJdbcInterface getObjetoCLIRMI(){
        ConsultaJdbcInterface ServRem = null;
        try{
            ServRem = ( ConsultaJdbcInterface ) java.rmi.Naming.lookup( "//" + this.getRmiHost() + 
                        ":" + this.getPort() + "/" + this.getService() );
        }catch( Exception er ){
            System.out.println( "Se produjo un error en la llamada" +er.getMessage());
            er.printStackTrace();            
        }
        return ServRem;
    }    
    
    private String rmiHost;
    private String port;
    private String service;       
}
